package com.xuetang9.fujian.dao;

import com.xuetang9.fujian.jedis.DBConnection;
import com.xuetang9.fujian.model.User;

/**
 * @describe:
 * @author:fujian
 * @date:2021/6/13
 * @Version: V1.0
 * @Package: com.xuetang9.fujian.dao
 */
@SuppressWarnings("all")
public class UserDao {
    private DBConnection dbConnection = new DBConnection();
    /**
     * @desc product user account
     * @param null
     * @return
     * @author fujian
     * @date 2021/6/13 09:40:31
     * @version 1.0
     **/
    private String creatAccount(){
        String account = null;
        if (dbConnection.connect()){
            Object data = dbConnection.get("user_account");
            Integer accountNumber;
            if (data == null){
                accountNumber = 1;
            }else {
                accountNumber = (Integer) data;
                accountNumber ++;
            }
            dbConnection.save("user_account",accountNumber);
            account = String.format("A%5d",accountNumber.intValue());
            account = account.replace(" ","0");
            dbConnection.close();
        }
        return account;
    }

    public void save(User user){
        if (user.getAccount() == null){
            String account = creatAccount();
            System.out.println("accountNumber = " + account);
            user.setAccount(account);
        }
        if (dbConnection.connect()){
            dbConnection.save(user.getAccount(),user);
            dbConnection.close();
        }
    }

    public User get(String account){
        if (dbConnection.connect()){
            Object data = dbConnection.get(account);
            dbConnection.close();
            return (User) data;
        }
        return null;
    }

    public static void main(String[] args) {
        UserDao userDao = new UserDao();
//        User user = new User();
//        user.setNickName("fujian");
//        user.setPassword("123456");
//        user.setSignature("signature");
//        user.setSex("male");
//        userDao.save(user);
        User user1 = userDao.get("A00001");
        System.out.println(user1);
    }
}

